package com.zito.health.service.impl;

import com.zito.health.entity.ExtensionTrainPersonnel;
import com.zito.health.mapper.ExtensionTrainPersonnelMapper;
import com.zito.health.service.ExtensionTrainPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Sam on 2020/03/14.
 */
@Service
public class ExtensionTrainPersonnelServiceImpl implements ExtensionTrainPersonnelService {

    @Autowired
    private ExtensionTrainPersonnelMapper extensionTrainPersonnelMapper;


    @Override
    public ExtensionTrainPersonnel save(ExtensionTrainPersonnel object) {
        return null;
    }

    @Override
    public void delete(Integer id) {
        extensionTrainPersonnelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(ExtensionTrainPersonnel object) {
        extensionTrainPersonnelMapper.updateByPrimaryKeySelective(object);

    }

    @Override
    public List<ExtensionTrainPersonnel> query() {
        return null;
    }

    @Override
    public List<ExtensionTrainPersonnel> query(ExtensionTrainPersonnel object) {
        return extensionTrainPersonnelMapper.select(object);
    }

    @Override
    public ExtensionTrainPersonnel queryOne(ExtensionTrainPersonnel object) {
        return null;
    }

    @Override
    public ExtensionTrainPersonnel queryById(Integer id) {
        return extensionTrainPersonnelMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean exists(Integer id) {
        return false;
    }

    @Override
    public int deleteByIds(String ids) {
        String[] id = ids.split(",");
        List list = new ArrayList();
        Integer id1;
        for (int i = 0; i < id.length; i++) {
            id1 = Integer.parseInt(id[i]);
            list.add(id1);
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println("list内的存的id有:" + list.get(i));
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", list);
        return extensionTrainPersonnelMapper.deleteByIds(map);
    }

    @Override
    public void insertImport(List<Map<String, Object>> list) {
        ExtensionTrainPersonnel let;
        List lista = new ArrayList();
        if (list != null && !"".equals(list)) {
            int j = 0;
            int tiao = list.size() / 13;
            for (int i = 0; i < tiao; i++) {

                for (j = i; j < list.size(); j += (list.size() / 13)) {
                    lista.add(list.get(j));
                }

                Integer trainId = (Integer) ((Map) lista.get(0)).get("trainId");
                String name = (String) ((Map) lista.get(1)).get("name");
                Integer education = (Integer) ((Map) lista.get(2)).get("education");
                Integer areaId = (Integer) ((Map) lista.get(3)).get("areaId");
                Integer ages = (Integer) ((Map) lista.get(4)).get("ages");
                String sid = (String) ((Map) lista.get(5)).get("sid");
                String phone = (String) ((Map) lista.get(6)).get("phone");
                float scoreTrain = (Float) ((Map) lista.get(7)).get("scoreTrain");
                float scoreIncharge = (Float) ((Map) lista.get(8)).get("scoreIncharge");
                float scorePersonnel = (Float) ((Map) lista.get(9)).get("scorePersonnel");
                String department = (String) ((Map) lista.get(10)).get("department");
                String title = (String) ((Map) lista.get(11)).get("title");
                String major = (String) ((Map) lista.get(12)).get("major");

                let = new ExtensionTrainPersonnel();
                let.setAges(ages);
                let.setAreaId(areaId);
                let.setTrainId(trainId);
                let.setName(name);
                let.setEducation(education);
                let.setSid(sid);
                let.setPhone(phone);
                let.setScoreIncharge((float) scoreIncharge);
                let.setScorePersonnel((float) scorePersonnel);
                let.setScoreTrain((float) scoreTrain);
                let.setDepartment(department);
                let.setTitle(title);
                let.setMajor(major);


                Integer sid1 = Integer.parseInt(sid.substring(sid.length() - 2, sid.length() - 1));
                if (sid1 % 2 == 0) {
                    let.setSex(2);
                } else {
                    let.setSex(1);
                }

                extensionTrainPersonnelMapper.insertSelective(let);
                lista.clear();
            }

        }
    }

    @Override
    public int selectSidCount(Map<String, Object> param) {
        return extensionTrainPersonnelMapper.selectSidCount(param);
    }
}
